package com.lfg.leetcode.leetcode0;


public class No48rotate {

    public static void main(String[] args) {
        int[][] array = new int[3][3];


        array[0] = new int[]{1, 2, 3};
        array[1] = new int[]{5, 6, 7};
        array[2] = new int[]{9, 10, 11};
//        array[0] = new int[]{1, 2, 3, 4};
//        array[1] = new int[]{5, 6, 7,8};
//        array[2] = new int[]{9,10,11,12};
//        array[3] = new int[]{13,14,15,16};

        rotate(array);
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void rotate2(int[][] matrix) {
        int length = matrix.length;
        int half = length / 2;
        for (int i = 0; i <= half; i++) {
            for (int j = i; j < length - i - 1; j++) {
                int oi = i;
                int oj = j;
                int di;
                int dj;
                int tmp = matrix[oi][oj];
                int num = 0;
                for (int n = 0; n < 4; n++) {
                    di = oj;
                    dj = length - oi - 1;
                    num = matrix[di][dj];
                    matrix[di][dj] = tmp;
                    tmp = num;
                    oi = di;
                    oj = dj;
                }
            }
        }
    }

    public static void rotate(int[][] matrix) {
        int length = matrix.length - 1;
        int[][] newMatrix = new int[matrix.length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                newMatrix[j][length - i] = matrix[i][j];
            }
        }

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                matrix[i][j] = newMatrix[i][j];
            }
        }
    }

}

